<?php

class CFavor extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	
	public function __construct($type){
		
		$this->_name=R_PRE . $type . "_favors";
		
		parent::__construct();
		
	}
	
	public function createFavor($data){
		
		try{
		
			$this->insert($data);

			return true;
			
		}catch(Exception $e){
			
			$e;
			
		}
		
		return false;
		
	}
	
	public function getFavorCounts($fieldArray=null){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'COUNT(*)');
		
		if($fieldArray!=null){
		
			foreach ($fieldArray as $field => $value){
				
				$where = $db->quoteInto("$field = ?", $value);
				
				$select->where($where);
				
			}
		
		}
		
		return $db->fetchOne($select);
		
		
	}
	
	public function getFavors($uid){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,"*");
		
		if($this->_name==R_PRE.'item_favors'){
			
			$select->join(R_PRE.'items',R_PRE.'items.itemid='.$this->_name.'.itemid',array('itemid','subject'));
			
		}else if($this->_name==R_PRE.'cate_favors'){
			
			$select->join(R_PRE.'productfls',R_PRE.'productfls.flid='.$this->_name.'.flid','*');
			
		}else if($this->_name==R_PRE.'member_favors'){
			
			$select->join(R_PRE.'members',R_PRE.'members.uid='.$this->_name.'.favor_uid','*');
			
		}
		
		$select->where($this->_name.'.uid=?',$uid);

		$thisArray=$db->fetchAll($select);
		
		return $thisArray;
		
	}
	
	public function delFavor($field,$valueArray){
		
		$db=$this->getAdapter();
		
		$valueString=join(',',$valueArray);

		$db->delete($this->_name,"$field IN ($valueString)");
		
	}
	
	

	
}